package com.elong.android.specialhouse.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.base.BaseVolleyActivity;
import com.elong.android.specialhouse.entity.FilterCondition;
import com.elong.android.specialhouse.ui.FilterAddSubView;
import com.elong.android.specialhouse.ui.RangeSeekBar;
import com.elong.android.specialhouse.ui.RangeSeekBar.OnRangeSeekBarChangeListener;
import com.elong.android.specialhouse.ui.RangeSeekBar.Thumb;
import com.elong.android.specialhouse.utils.EventReportTools;
import com.elong.framework.netmid.response.IResponse;

import java.util.ArrayList;
import java.util.List;

public class ApartmentFilterActivity extends BaseVolleyActivity<IResponse<?>> implements OnCheckedChangeListener{
	private static final String PAGE_NAME = "youfangFilterPage";
	private RelativeLayout rlRangeSeekbar;
	private TextView tvPriceRange;
	private TextView tvCommonReset, tvFilterConfirm;
	private CheckBox ckHouseTypeFull, ckHouseTypeRoom, ckHouseTypeBed;
	private RangeSeekBar<Integer> mSeekBar;
	private FilterAddSubView peopleAddSubView, bedAddSubView;
	private FilterCondition mFilterCondition = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        EventReportTools.sendPageOpenEvent(PAGE_NAME);
		mFilterCondition = (FilterCondition) getIntent().getSerializableExtra(
				"FilterCondition");
		mSeekBar = new RangeSeekBar<Integer>(FilterCondition.PRICE_MIN,
				FilterCondition.PRICE_MAX / 10, this);
		rlRangeSeekbar.addView(mSeekBar);
		mSeekBar.setNotifyWhileDragging(true);
		mSeekBar.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {

			@Override
			public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar,
													Thumb thumb, Integer oldminValue, Integer oldmaxValue,
													Integer minValue, Integer maxValue) {
				if (thumb == RangeSeekBar.Thumb.MIN
						&& Math.abs(maxValue - minValue) <= 10
						&& minValue <= 100) {
					maxValue = minValue + 10;
					mSeekBar.setSelectedMinValue(minValue);
					mSeekBar.setSelectedMaxValue(maxValue);
				} else if (minValue > 100) {
					minValue = 100;
					maxValue = 110;
					mSeekBar.setSelectedMinValue(minValue);
					mSeekBar.setSelectedMaxValue(maxValue);
				}
				if (thumb == RangeSeekBar.Thumb.MAX
						&& Math.abs(maxValue - minValue) <= 10 && maxValue > 10) {
					minValue = maxValue - 10;
					mSeekBar.setSelectedMinValue(minValue);
					mSeekBar.setSelectedMaxValue(maxValue);
				} else if (maxValue < 10) {
					minValue = 0;
					maxValue = 10;
					mSeekBar.setSelectedMinValue(minValue);
					mSeekBar.setSelectedMaxValue(maxValue);
				}
				tvPriceRange.setText(getPriceText(minValue * 10, maxValue * 10));

			}
		});

		if (mFilterCondition == null) {
			mFilterCondition = new FilterCondition();
		} else {
			setData();
		}
	}

	private void setData() {
		mSeekBar.setSelectedMinValue(mFilterCondition.getLowestPrice() / 10);
		mSeekBar.setSelectedMaxValue(mFilterCondition.getHighestPrice() / 10);
		tvPriceRange.setText(getPriceText(mFilterCondition.getLowestPrice(), mFilterCondition.getHighestPrice()));
		if (mFilterCondition.getRentalType() != null) {
			for (int i = 0; i < mFilterCondition.getRentalType().size(); i++) {
				if (mFilterCondition.getRentalType().get(i) == 0) {
					ckHouseTypeFull.setChecked(true);
				}
				if (mFilterCondition.getRentalType().get(i) == 1) {
					ckHouseTypeRoom.setChecked(true);
				}
				if (mFilterCondition.getRentalType().get(i) == 2) {
					ckHouseTypeBed.setChecked(true);
				}
			}
		}
		peopleAddSubView.setCount(mFilterCondition.getNumberOfPeople());
		bedAddSubView.setCount(mFilterCondition.getNumberOfBeds());
	}
	/**
	 * 获取显示的价格区间字符串
	 */
	private String getPriceText(int lowestPrice , int highestPrice){
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("每晚¥");
		stringBuffer.append(lowestPrice);
		stringBuffer.append(" - ¥");
		if(highestPrice > 1000){
			stringBuffer.append("1000+");
		}else{
			stringBuffer.append(highestPrice);
		}
		return  stringBuffer.toString();
	}
	@Override
	protected void initContentView() {
		setContentView(R.layout.act_apartment_filter);
		setHeader(R.string.filter);
		rlRangeSeekbar = (RelativeLayout) findViewById(R.id.rangeseekbarlayout);
		tvPriceRange = (TextView) findViewById(R.id.price_range);
		tvCommonReset = (TextView) findViewById(R.id.common_reset);
		tvCommonReset.setVisibility(View.VISIBLE);
		ckHouseTypeFull = (CheckBox) findViewById(R.id.ck_house_type_full);
		ckHouseTypeRoom = (CheckBox) findViewById(R.id.ck_house_type_room);
		ckHouseTypeBed = (CheckBox) findViewById(R.id.ck_house_type_bed);
		tvFilterConfirm = (TextView) findViewById(R.id.tv_filter_confirm);
		peopleAddSubView = (FilterAddSubView) findViewById(R.id.peopleAddSubView);
		bedAddSubView = (FilterAddSubView) findViewById(R.id.bedAddSubView);
		bedAddSubView.setMin(FilterCondition.DEFAULT_BED_MIN);
		tvCommonReset.setOnClickListener(this);
		tvFilterConfirm.setOnClickListener(this);
		ckHouseTypeFull.setOnCheckedChangeListener(this);
		ckHouseTypeRoom.setOnCheckedChangeListener(this);
		ckHouseTypeBed.setOnCheckedChangeListener(this);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()){
			case R.id.ck_house_type_full:
				EventReportTools.sendPageSpotEvent(PAGE_NAME, "whole");//整套whole
			    break;
			case R.id.ck_house_type_room:
				EventReportTools.sendPageSpotEvent(PAGE_NAME, "single");//单间single
				break;
			case R.id.ck_house_type_bed:
				EventReportTools.sendPageSpotEvent(PAGE_NAME, "bed");//床位bed
				break;
		}
	}

	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.common_reset:
			EventReportTools.sendPageSpotEvent(PAGE_NAME, "reset");//重置reset
			filterReset();
			break;
		case R.id.tv_filter_confirm:
			EventReportTools.sendPageSpotEvent(PAGE_NAME, "OK");//确认OK
			backWithResult();
			break;
		case R.id.common_head_back:
			EventReportTools.sendPageSpotEvent(PAGE_NAME, "back");//返回back
		    back();
		    break;
		}
	}
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			EventReportTools.sendPageSpotEvent(PAGE_NAME, "back");//返回back
			back();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	/**
	 * 返回筛选参数
	 */
	private void backWithResult() {
		Intent intent = new Intent();
		mFilterCondition.setLowestPrice(mSeekBar.getSelectedMinValue() * 10);
		mFilterCondition.setHighestPrice(mSeekBar.getSelectedMaxValue() * 10);
		mFilterCondition.setNumberOfBeds((short) bedAddSubView.getCount());
		mFilterCondition.setNumberOfPeople((short) peopleAddSubView.getCount());
		List<Integer> mRentalType = new ArrayList<Integer>();
		if (ckHouseTypeFull.isChecked()) {
			mRentalType.add(0);
		}
		if (ckHouseTypeRoom.isChecked()) {
			mRentalType.add(1);
		}
		if (ckHouseTypeBed.isChecked()) {
			mRentalType.add(2);
		}
		mFilterCondition.setRentalType(mRentalType);
		intent.putExtra("FilterCondition", mFilterCondition);
		setResult(Activity.RESULT_OK, intent);
		back();
	}

	/**
	 * 重置筛选
	 */
	private void filterReset() {
		mSeekBar.setSelectedMinValue(FilterCondition.PRICE_MIN);
		mSeekBar.setSelectedMaxValue(FilterCondition.PRICE_MAX / 10);
		tvPriceRange.setText(R.string.price_range);
		ckHouseTypeFull.setChecked(false);
		ckHouseTypeRoom.setChecked(false);
		ckHouseTypeBed.setChecked(false);
		peopleAddSubView.setCount(FilterCondition.DEFAULT_PEOPLE_MIN);
		bedAddSubView.setCount(FilterCondition.DEFAULT_BED_MIN);
		mFilterCondition.clear();
	}
}
